package com.example.second.proxy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.second.R;
import com.example.second.proxy.classes.StaticStarProxy;
import com.example.second.proxy.classes.SuperStarADao;
import com.example.second.proxy.interfaces.IStarDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyActivity extends AppCompatActivity {
    private static final String TAG = "ProxyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxy);

        staticProxy();
        Log.d(TAG, "onCreate: -------------------");
        dynamicProxy();

    }

    private void dynamicProxy() {
        SuperStarADao huge = new SuperStarADao("huge");
        IStarDao dynamicStarProxy = (IStarDao) Proxy.newProxyInstance(
                huge.getClass().getClassLoader(),
                new Class<?>[] { IStarDao.class },
//                huge.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Log.d(TAG, "invoke: " + "前期动态宣传");
                        Object invoke = method.invoke(huge, args);
                        Log.d(TAG, "invoke: " + "后期动态宣传");
                        return invoke;
                    }
                }
        );
        dynamicStarProxy.doWork();
    }

    private void staticProxy() {
        SuperStarADao huge = new SuperStarADao("huge");
        StaticStarProxy staticStarProxy = new StaticStarProxy(huge);
        staticStarProxy.doWork();
    }
}